let app = getApp()

Page({

  //页面的初始数据
  data: {
    oldpwd: '',
    newpwd: '',
    confirmpwd: ''
  },

  formSubmit(e) {
    console.log("app", app)
    let oldpwd = e.detail.value.oldpwd
    let newpwd = e.detail.value.newpwd
    let confirmpwd = e.detail.value.confirmpwd

    if (oldpwd == '' || app.trim(newpwd) == '' || app.trim(confirmpwd) == '') {
      dd.alert({
        content: '密码不可为空或空格，请重新输入',
        buttonText: '确定'
      })
    } else if (oldpwd == newpwd) {
      dd.alert({
        content: '新密码与旧密码相同，请重新输入新密码',
        buttonText: '确定'
      })
    } else if (newpwd != confirmpwd) {
      dd.alert({
        content: '两次输入新密码不一致，请重新输入',
        buttonText: '确定'
      })
    } else {
      dd.httpRequest({
        headers: {
          "Content-Type": "application/json"
        },
        url: app.userUrl + '/DingDing/AccountManagement/updateUserPassWord',
        method: 'POST',
        data: JSON.stringify({
          username: app.globalData.userName,
          oldpwd: oldpwd,
          newpwd: newpwd
        }),
        dataType: 'json',
        success: (res) => {
          console.log(res)
          if (res.data.res == "success") {
            dd.alert({
              content: res.data.msg + ", 点击确定后重新登录",
              success: (res) => {
                dd.reLaunch({
                  url: '/page/login/login'
                })
              }
            })
          } else {
            dd.alert({
              content: res.data.msg,
              buttonText: '确定'
            })
          }
        },
        fail: (res) => {
            dd.showToast({
              title: '网络异常！',
              type: 'fail'
            })
        }
      })
    }
  },

  onLoad(options) {
    console.log(options);
    this.setData({
      title: options.title
    });
  },
});